package model

import (
	"gorm.io/gorm"
	"shopping_08_20/globale"
	"time"
)

type Comment struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:评论id;primaryKey;not null;" json:"id"`  // 评论id
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`            // 用户ID
	ShopId    int32          `gorm:"column:shop_id;type:int;comment:商品ID;not null;" json:"shop_id"`            // 商品ID
	Content   string         `gorm:"column:content;type:varchar(300);comment:评论内容;not null;" json:"content"` // 评论内容
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (c *Comment) TableName() string {
	return "comment"
}

// 添加评论
func (c *Comment) AddComment() error {
	return globale.DB.Create(&c).Error
}

// 查看商品评论
func (c *Comment) ShopFinComment() (comment []Comment, err error) {
	err = globale.DB.Where("shop_id = ?", c.ShopId).Find(&comment).Error
	return
}

// 修改评论
func (c *Comment) UpdateComment() error {
	return globale.DB.Updates(&c).Error
}

// 删除评论
func (c *Comment) DeleteComment() error {
	return globale.DB.Delete(&c).Error
}

// 查看评论
func (c *Comment) IdGetComment() error {
	return globale.DB.Where("id = ?", c.Id).Find(&c).Error

}
